home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 1 / Mac Magazin and MacEasy Magazine CD - Issue 01.iso / Sharewarebibliothek / Powermac / C64 / SOURCE / Memory.c < prev    next >
Text File  |  1994-06-06  |  6KB  |  199 lines

  1. /*
  2.     Commodore 64 Emulator v0.4      Earle F. Philhower III 
  3.     Copyright (C) 1993-4            (st916w9r@dunx1.ocs.drexel.edu)
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19.  
  20. #include "Processor.h"
  21. #include "Error.h"
  22. #include "Resources.h"
  23. #include "Memory.h"
  24.  
  25.  
  26. /* Global memory pointers */
  27. byte *RAM=nil, *loROM=nil, *hiROM=nil, *charROM=nil;
  28. byte *RAMp1=nil, *loROMp1=nil, *hiROMp1=nil, *charROMp1=nil;
  29. byte **memory=nil, **memoryp1=nil;
  30.  
  31. /* Used for the memory mapping routines locally */
  32. static byte **loRAMstore=nil, **loROMstore=nil;
  33. static byte **hiRAMstore=nil, **hiROMstore=nil;
  34. static byte **charRAMstore=nil, **charROMstore=nil;
  35.  
  36.  
  37. int MemoryInitialize(void)
  38. {
  39.     Handle temp;
  40.     word x;
  41.     
  42.     /* Get space for RAM and ROMS */
  43.     RAM=(byte *)GetMemory(0x10001);
  44.     loROM=(byte *)GetMemory(0x2000);
  45.     charROM=(byte *)GetMemory(0x1000);
  46.     hiROM=(byte *)GetMemory(0x2000);
  47.     if ((RAM==nil)||(loROM==nil)||(charROM==nil)||(hiROM==nil))
  48.         return(kOutOfMemory);
  49.     
  50.     /* Load the ROMS into the memory allocated */
  51.     temp=GetResource('ROMS', kBASICROM);    /* BASIC */
  52.     if (temp==nil) return(kROMMissing);
  53.     BlockMove(*temp, loROM, 0x2000);
  54.     ReleaseResource(temp);
  55.     
  56.     temp=GetResource('ROMS', kCHARROM);    /* CHARACTERS */
  57.     if (temp==nil) return(kROMMissing);
  58.     BlockMove(*temp, charROM, 0x1000);
  59.     ReleaseResource(temp);
  60.     
  61.     temp=GetResource('ROMS', kKERNELROM);    /* KERNAL */
  62.     if (temp==nil) return(kROMMissing);
  63.     BlockMove(*temp, hiROM, 0x2000);
  64.     ReleaseResource(temp);
  65.  
  66.     /* Set up the hi-byte shortcut pointers */
  67.     RAMp1=&RAM[1];
  68.     loROMp1=&loROM[1];
  69.     charROMp1=&charROM[1];
  70.     hiROMp1=&hiROM[1];
  71.     
  72.     /* Get the double-dereference memory map space */
  73.     memory=(byte**)GetMemory(0x10001*sizeof(byte *));
  74.     if (memory!=nil) memoryp1=&(memory[1]);
  75.     loRAMstore=(byte**)GetMemory(0x2000*sizeof(byte *));
  76.     loROMstore=(byte**)GetMemory(0x2000*sizeof(byte *));
  77.     charRAMstore=(byte**)GetMemory(0x1000*sizeof(byte *));
  78.     charROMstore=(byte**)GetMemory(0x1000*sizeof(byte *));
  79.     hiRAMstore=(byte**)GetMemory(0x2000*sizeof(byte *));
  80.     hiROMstore=(byte**)GetMemory(0x2000*sizeof(byte *));
  81.     if ((loRAMstore==nil)||(loROMstore==nil)||(charRAMstore==nil)||
  82.         (charROMstore==nil)||(hiRAMstore==nil)||(hiROMstore==nil)||
  83.         (memory==nil))
  84.         return(kOutOfMemory);
  85.     
  86.     /* Set up the standard RAM pointers into our memory[] */
  87.     for (x=0x0000; x<0xffff; x++) memory[x]=&RAM[x];
  88.     memory[x]=&(RAM[x]);    /* Takes care of overflow */
  89.  
  90.     /* Copy the ROM and RAM pointers into our holding places */
  91.     for (x=0x0000; x<0x2000; x++) {
  92.         loROMstore[x]=&loROM[x];
  93.         loRAMstore[x]=&(RAM[0xa000+x]); }
  94.     for (x=0x0000; x<0x1000; x++) {
  95.         charROMstore[x]=&charROM[x];
  96.         charRAMstore[x]=&(RAM[0xd000+x]); }
  97.     for (x=0x0000; x<0x2000; x++) {
  98.         hiROMstore[x]=&hiROM[x];
  99.         hiRAMstore[x]=&(RAM[0xe000+x]); }
  100.     
  101.     return(kNoError);
  102. }
  103.  
  104. void MemoryCleanUp(void)
  105. {
  106.     if (RAM != nil) FreeMemory(RAM);
  107.     if (loROM != nil) FreeMemory(loROM);
  108.     if (charROM != nil) FreeMemory(charROM);
  109.     if (hiROM != nil) FreeMemory(hiROM);
  110.     if (memory != nil) FreeMemory(memory);
  111.     if (loRAMstore != nil) FreeMemory(loRAMstore);
  112.     if (loROMstore != nil) FreeMemory(loROMstore);
  113.     if (charRAMstore != nil) FreeMemory(charRAMstore);
  114.     if (charROMstore != nil) FreeMemory(charROMstore);
  115.     if (hiRAMstore != nil) FreeMemory(hiRAMstore);
  116.     if (hiROMstore != nil) FreeMemory(hiROMstore);
  117. }
  118.  
  119. void SetUpMemoryMap(void)
  120. {
  121.     if (*RAMp1&(1<<0)) BlockMove(loROMstore, &memory[0xa000],(0x2000*sizeof(byte *)));
  122.     else BlockMove(loRAMstore, &memory[0xa000],(0x2000*sizeof(byte *)));
  123.  
  124.     if (*RAMp1&(1<<1)) BlockMove(charRAMstore, &memory[0xd000],(0x1000*sizeof(byte *)));
  125.     else BlockMove(charROMstore, &memory[0xd000],(0x1000*sizeof(byte *)));
  126.  
  127.     if (*RAMp1&(1<<2)) BlockMove(hiROMstore, &memory[0xe000],(0x2000*sizeof(byte *)));
  128.     else BlockMove(hiRAMstore, &memory[0xe000],(0x2000*sizeof(byte *)));
  129.  
  130.     CheckMemory();
  131. }
  132.  
  133.  
  134. #define SIGNATURE 'BoMb'
  135. #define MAXSLOTS 64
  136. typedef unsigned long SignatureType;
  137. typedef struct {
  138.     byte *startOfMem, *givenMemPtr;
  139.     SignatureType id;
  140. } Signature;
  141. static Signature signature[MAXSLOTS];
  142. static byte numSignatures=0;
  143.  
  144. byte *GetMemory(unsigned long size)
  145. {
  146.     byte *mem;
  147.     
  148.     /* First see if we've got room in our memory pointers */
  149.     if (numSignatures==MAXSLOTS) InternalError(kOutOfMemoryPointers);
  150.     
  151.     /* Get memory from heap, including space for our signature */
  152.     mem=(byte *)NewPtrClear(size+sizeof(SignatureType));
  153.     if (mem==nil) return (byte *)nil;
  154.     
  155.     /* Set up our memory structure */
  156.     signature[numSignatures].startOfMem=mem;
  157.     signature[numSignatures].givenMemPtr=&(mem[sizeof(SignatureType)]);
  158.     signature[numSignatures].id=SIGNATURE;
  159.     
  160.     /* Store out "sentinel" bytes that SHOULD NOT BE OVERWRITTEN */
  161.     BlockMove(&(signature[numSignatures].id), mem, sizeof(SignatureType));
  162.     
  163.     /* Return address just after out sentinel */
  164.     return signature[numSignatures++].givenMemPtr;
  165. }
  166.  
  167. void FreeMemory(void *mem)
  168. {
  169.     int x, y;
  170.     byte *memory=(byte *)mem;
  171.     
  172.     /* Search for the passed memory pointer in the list of ones we've given */
  173.     for (x=0; x<numSignatures; x++)
  174.         if (memory==signature[x].givenMemPtr)
  175.         {
  176.             /* Found a match, free the memory */
  177.             DisposePtr((Ptr)signature[x].startOfMem);
  178.             /* Shift out pointers down to fill the vacant slot */
  179.             for (y=x; y<numSignatures; y++) signature[y]=signature[y+1];
  180.             numSignatures--;
  181.             return;
  182.         }
  183.     /* Didn't find the memory, so exit gracefully */
  184.     InternalError(kInvalidMemoryFree);
  185. }
  186.  
  187. void CheckMemory(void)
  188. {
  189.     int x;
  190.     SignatureType sign;
  191.     
  192.     /* Scan all of out given signatures for the sentinel value */
  193.     for (x=0; x<numSignatures; x++)
  194.     {
  195.         sign=*(SignatureType *)signature[x].startOfMem;
  196.         if (sign!=signature[x].id) InternalError(kMemoryViolation);
  197.     }
  198. }
  199.